package com.jindu.producer.system.controller;

import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.system.jpa.MenuJpa;
import com.jindu.producer.system.jpa.RoleMenuJpa;
import com.jindu.producer.system.model.SMenuEntity;
import com.jindu.producer.system.model.SRoleMenuEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jarvis
 * @date 2018/2/21
 */
@Api(description = "角色和菜单权限配置信息", tags = "RoleMenuController", basePath = "/roleMenu")
@RestController
@RequestMapping(value = "/roleMenu",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RoleMenuController {

    @Autowired
    RoleMenuJpa roleMenuJpa;
    @Autowired
    MenuJpa menuJpa;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据角色的ID，查询关联的配置表，匹配是否有权限", notes = "返回带有是否选中状态的菜单树")
    @RequestMapping(value = "getMenuTreeByRoleId",method = RequestMethod.GET)
    public JsonResult<List<SMenuEntity>> getMenuTreeByRoleId(String roleId){
        List<SMenuEntity> menuEntities=menuJpa.findAll();
        List<SRoleMenuEntity> roleMenuEntities=roleMenuJpa.findByRoleId(roleId);

        List<SMenuEntity> sMenuTreeEntity=new ArrayList<>();
        List<SMenuEntity> root =menuEntities.stream()
                .filter(s -> s.getParentId().equals("0"))
                .collect(Collectors.toList());

        for (SMenuEntity menu : root){
            List<SMenuEntity> subNode=menuEntities.stream()
                    .filter(s->s.getParentId().equals(menu.getId()))
                    .collect(Collectors.toList());
            for (SRoleMenuEntity rm : roleMenuEntities) {
                //根据菜单的id，标记根节点的选中状态
                if(rm.getMenuId().equals(menu.getId())){
                    menu.setChecked(true);
                }
                for (SMenuEntity sub :subNode) {
                    if(sub.getId().equals(rm.getMenuId())){
                        sub.setChecked(true);
                    }
                }
            }
            menu.setSubNode(subNode);
            sMenuTreeEntity.add(menu);
        }
        return JsonResult.getSuccessResult(sMenuTreeEntity);
    }

    @ApiOperation(value = "批量保存和修改角色菜单的数据信息", notes = "返回保存和修改的角色菜单配置信息")
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public JsonResult<Integer> save(@RequestBody List<SRoleMenuEntity> sRoleMenuEntities){
        try {
            if(sRoleMenuEntities.size()>0){
                String roleId=sRoleMenuEntities.stream().findFirst().get().getRoleId();
                roleMenuJpa.deleteByRoleId(roleId);

                for (SRoleMenuEntity roleMenuEntity : sRoleMenuEntities) {
                    roleMenuEntity.setId(IdGenerator.uuid());
                    roleMenuEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
                   roleMenuJpa.saveAndFlush(roleMenuEntity);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonResult.getSuccessResult(1);
    }

}
